#include "actioncompositekey.h"
#include <Windows.h>

char descAck[][36]
{
    "Ctrl+A",
    "WIN+D",

    "exceed.."
};
ActionCompositeKey::ActionCompositeKey(ActionComposite type)
{
    classname = __FUNCTION__;
    actParam = QJsonObject();
    actParam.insert("type",(int)type);
}

int ActionCompositeKey::execute()
{
    ActionComposite ac = (ActionComposite)actParam.value("type").toInt();
    qInfo()<<classname<<"Composite"<<descAck[ac];

    INPUT input[4];
    memset(input,0,sizeof(INPUT)*4);
    int inputIndex = 0;
    DWORD timestamp = 1;
    if(ac == ACKSelectAll)
    {
        input[inputIndex].type = INPUT_KEYBOARD;
        input[inputIndex].ki.dwFlags = 0;
        input[inputIndex].ki.wVk = VK_CONTROL;
        input[inputIndex].ki.time = timestamp;
        ++inputIndex; timestamp+=10;

        input[inputIndex].type = INPUT_KEYBOARD;
        input[inputIndex].ki.dwFlags = 0;
        input[inputIndex].ki.wVk = 'A';
        input[inputIndex].ki.time = timestamp;
        ++inputIndex; timestamp+=10;

        input[inputIndex].type = INPUT_KEYBOARD;
        input[inputIndex].ki.dwFlags = KEYEVENTF_KEYUP;
        input[inputIndex].ki.wVk = 'A';
        input[inputIndex].ki.time = timestamp;
        ++inputIndex; timestamp+=10;

        input[inputIndex].type = INPUT_KEYBOARD;
        input[inputIndex].ki.dwFlags = KEYEVENTF_KEYUP;
        input[inputIndex].ki.wVk = VK_CONTROL;
        input[inputIndex].ki.time = timestamp;
        ++inputIndex; timestamp+=10;

        ::SendInput(inputIndex,input,sizeof(INPUT));
        QThread::msleep(100);
        return 0;
    }

    if(ac == ACKToggleDesktop)
    {

        input[inputIndex].type = INPUT_KEYBOARD;
        input[inputIndex].ki.dwFlags = 0;
        input[inputIndex].ki.wVk = VK_LWIN;
        input[inputIndex].ki.time = timestamp;
        ++inputIndex; timestamp+=10;

        input[inputIndex].type = INPUT_KEYBOARD;
        input[inputIndex].ki.dwFlags = 0;
        input[inputIndex].ki.wVk = 'A';
        input[inputIndex].ki.time = timestamp;
        ++inputIndex; timestamp+=10;

        input[inputIndex].type = INPUT_KEYBOARD;
        input[inputIndex].ki.dwFlags = KEYEVENTF_KEYUP;
        input[inputIndex].ki.wVk = 'A';
        input[inputIndex].ki.time = timestamp;
        ++inputIndex; timestamp+=10;

        input[inputIndex].type = INPUT_KEYBOARD;
        input[inputIndex].ki.dwFlags = KEYEVENTF_KEYUP;
        input[inputIndex].ki.wVk = VK_LWIN;
        input[inputIndex].ki.time = timestamp;
        ++inputIndex; timestamp+=10;

        ::SendInput(inputIndex,input,sizeof(INPUT));
        QThread::msleep(500);
        return 0;
    }

    message = "not defined composite type.";
    return -1;
}
